// JA2IniEditorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "JA2IniEditor.h"
#include "JA2IniEditorDlg.h"
#include "ProgressBarDialog.h"
#include ".\ja2inieditordlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

ULONG_PTR gdiplusToken;

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
  afx_msg void OnEnChangeEdit1();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_MOVE()
	ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()


// CJA2IniEditorDlg dialog



CJA2IniEditorDlg::CJA2IniEditorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CJA2IniEditorDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	pWeapDlg = NULL;
	m_bInit = false;
	m_Saved = 0;
	m_bWISaved = false;
	m_bIISaved = false;
	m_bAttSaved = false;
	m_bDealerSaved = false;
  m_bExplosivesSaved = false;
  m_bArmourSaved     = false;
}

CJA2IniEditorDlg::~CJA2IniEditorDlg()
{
  delete pWeapDlg;
  delete pWeapTTX;
  delete pAttachDlg;
  delete pItemsDlg;
  delete pDealerDlg;
  delete pExpl_Arm;
}

void CJA2IniEditorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, m_ctrlTab);
}

BEGIN_MESSAGE_MAP(CJA2IniEditorDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_MOVE()
	ON_WM_SHOWWINDOW()
	ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_WM_SIZING()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnTcnSelchangeTab1)
	ON_COMMAND(ID_EXPORT_WEAPONS, OnExportWeapons)
	ON_COMMAND(ID_EXPORT_ITEMS, OnExportItems)
	ON_COMMAND(ID_EXPORT_ATTACH, OnExportAttach)
	ON_COMMAND(ID_EXPORT_DEALER, OnExportDealer)
  ON_COMMAND(ID_SAVE_EXPL, &CJA2IniEditorDlg::OnSaveExpl)
  ON_COMMAND(ID_SAVE_ARMOUR, &CJA2IniEditorDlg::OnSaveArmour)
//  ON_WM_HELPINFO()
END_MESSAGE_MAP()


// CJA2IniEditorDlg message handlers

BOOL CJA2IniEditorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	AfxGetApp()->DoWaitCursor(1);
	// Add "About..." menu item to system menu.
  //Initialize GDI+
  GdiplusStartupInput gdiplusStartupInput; 
  GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

  // TODO: Add extra initialization here
	pWeapDlg = new CWeaponDlg();
	pWeapDlg->Create(IDD_WEAPONS,(CWnd *)&m_ctrlTab);
	pWeapTTX = new CWeapTTX();
	pWeapTTX->Create(IDD_WEAPTTX,(CWnd *)&m_ctrlTab);
	pItemsDlg = new CItemsDlg;
	pItemsDlg->Create(IDD_ITEMS, (CWnd *)&m_ctrlTab);
	pAttachDlg = new CAttachDlg();
	pAttachDlg->Create(IDD_ATTACH, (CWnd *)&m_ctrlTab);
	pDealerDlg = new CDealerDlg();
	pDealerDlg->Create(IDD_DEALERDLG, (CWnd *)&m_ctrlTab);
  pExpl_Arm = new CExpl_Arm();
  pExpl_Arm->Create(IDD_EXPL_ARM, (CWnd *)&m_ctrlTab);


  	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_WEAPON_PAGE),pWeapDlg);
	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_WEAPON_ADD_PAGE),pWeapTTX);
	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_ITEMS_PAGE),pItemsDlg);
	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_ATTACHES_PAGE),pAttachDlg);
	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_DEALERS),pDealerDlg);
	m_ctrlTab.AddTabPane(CString((LPCSTR)IDS_ARMOUR_EXPLOSIVES_PAGE),pExpl_Arm);
	cpPointers.pWeaponDlg = pWeapDlg;
	cpPointers.pWeapTTX   = pWeapTTX;
	cpPointers.pAttachDlg = pAttachDlg;
	cpPointers.pItemsDlg  = pItemsDlg;
	cpPointers.pDealerDlg  = pDealerDlg;
	pWeapDlg->SetClassPointers(&cpPointers);
	pWeapTTX->SetClassPointers(&cpPointers); 
	pItemsDlg->SetClassPointers(&cpPointers);
	pAttachDlg->SetClassPointers(&cpPointers);
	pDealerDlg->SetClassPointers(&cpPointers);
	pWeapTTX->FillWeaponStructures(&pWeapDlg->wspPointers);
  pExpl_Arm->SetClassPointers( &cpPointers );

  CProgressBarDialog dlgProgress;
  dlgProgress.Create( IDD_ProgressBar, (CWnd *)this);
  dlgProgress.ShowWindow( SW_SHOW );
	pItemsDlg->Load( &dlgProgress );
	pWeapDlg->Load( &dlgProgress );
	pWeapTTX->Load( &dlgProgress );
	pAttachDlg->Load( &dlgProgress );
	pDealerDlg->Load( &dlgProgress );
  pExpl_Arm->Load( &dlgProgress );
  dlgProgress.SetState( 100,"Parsing finished" );
  dlgProgress.ShowWindow( SW_HIDE );
  dlgProgress.DestroyWindow();
	AfxGetApp()->DoWaitCursor(-1);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJA2IniEditorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CJA2IniEditorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CJA2IniEditorDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CJA2IniEditorDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);
	m_ctrlTab.SetDefaultPane(0);
}

void CJA2IniEditorDlg::OnFileExit()
{
	// TODO: Add your command handler code here
  if (  !pWeapDlg->IsSaved() || 
        !pAttachDlg->IsSaved() || 
        !pItemsDlg->IsSaved() || 
        !pDealerDlg->IsSaved() || 
        !pExpl_Arm->IsSaved( true ) || 
        !pExpl_Arm->IsSaved( false ) )
    m_Saved = 0;
  else
    m_Saved = 1;

	if (!m_Saved)
	{
    int iRet = MessageBox("Save changes and exit?", "JA2 - Exit", MB_YESNOCANCEL);
    if ( iRet == IDCANCEL )
      return;
		if ( iRet == IDYES )
    {
      if ( !pWeapDlg->IsSaved() )         pWeapDlg->Save();
      if ( !pAttachDlg->IsSaved() )       pAttachDlg->Save();
      if ( !pItemsDlg->IsSaved() )        pItemsDlg->Save();
      if ( !pDealerDlg->IsSaved() )       pDealerDlg->Save();
      if ( !pExpl_Arm->IsSaved( true ) )  pExpl_Arm->Save( true );
      if ( !pExpl_Arm->IsSaved( false ) ) pExpl_Arm->Save( false );
    }
    GdiplusShutdown(gdiplusToken);
    EndDialog(0);
	}
	else 
  {
    GdiplusShutdown(gdiplusToken);
    EndDialog(0);
  }
}

void CJA2IniEditorDlg::OnOK()
{}
void CJA2IniEditorDlg::OnCancel()
{
	OnFileExit();
}

void CJA2IniEditorDlg::OnFileExport()
{
	
	/*pWeapDlg->Save();
	pItemsDlg->Save();
	MessageBox("Files succesfully saved.");*/
	//m_Saved = 1;
}

void CJA2IniEditorDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
	int i = m_ctrlTab.GetCurSel();
	if (i==0) 
	{
		CWeaponDlg *p = (CWeaponDlg *)m_ctrlTab.TabPanes.GetAt(i);
		p->ActualizeData();
	}
	*pResult = 0;
}

void CJA2IniEditorDlg::OnExportWeapons()
{
  if ( !pWeapDlg->IsSaved() )
	  pWeapDlg->Save();
	MessageBox("weapon.ini succesfully saved.");
}

void CJA2IniEditorDlg::OnExportItems()
{
  if ( !pItemsDlg->IsSaved() )
	  pItemsDlg->Save();
	MessageBox("items.ini succesfully saved.");
}

void CJA2IniEditorDlg::OnExportAttach()
{
  if ( !pAttachDlg->IsSaved() )
	  pAttachDlg->Save();
	MessageBox("attach.ini succesfully saved.");
}

void CJA2IniEditorDlg::OnExportDealer()
{
  if ( !pDealerDlg->IsSaved() )
	  pDealerDlg->Save();
	MessageBox("dealer.ini succesfully saved.");
}

void CJA2IniEditorDlg::OnSaveExpl()
{
  if ( !pExpl_Arm->IsSaved( true ) )
    pExpl_Arm->Save( true );
  MessageBox("expl.ini succesfully saved.");
}

void CJA2IniEditorDlg::OnSaveArmour()
{
  if ( !pExpl_Arm->IsSaved( false ) )
    pExpl_Arm->Save( false );
  MessageBox("armour.ini succesfully saved.");
}
